//
//  PywPayPwdSetTradePwdViewController.m
//  PywSdk
//
//  Created by smallwave on 16/8/12.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywPayPwdSetTradePwdViewController.h"
#import "PywMasonry.h"
#import "PywPayPwdHeaderView.h"
#import "PywPayPwdFooterView.h"
#import "PywPayPwdSetProblemViewCell.h"

@interface PywPayPwdSetTradePwdViewController ()<UITableViewDelegate, UITableViewDataSource, PywPayPwdFooterViewDelegate, PywPayPwdSetProblemViewCellDelegate>

@property (nonatomic, strong) UITableView           *tableView;
@property (nonatomic, strong) PywPayPwdHeaderView   *payPwdHeaderView;

@property (nonatomic, strong) PywPayPwdSetProblemViewCell *cell;

@property (nonatomic, copy) NSArray     *titleArray;        // 问题标题数组
@property (nonatomic, copy) NSArray     *placeholderArray;  // 占位字符数组
@property (nonatomic, copy) NSString    *payPwd;            // 支付密码

@end

@implementation PywPayPwdSetTradePwdViewController
KeyBoardHelper(85)

#pragma mark - Lift Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}
#pragma mark - PrepareUI
- (void)setUp{
    self.title = @"设置交易密码";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;

    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    [self.view addSubview:self.tableView];

    __weak typeof(self) wSelf = self;
    [self.tableView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        // 获取导航条高度
        CGRect rectNav = self.navigationController.navigationBar.frame;
        CGFloat nav_Height = rectNav.size.height;
        
        make.top.equalTo(wSelf.view.mas_top).offset(nav_Height);
        make.bottom.left.right.offset(0);
    }];
    
    // 添加手势
//    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom:)];
//    singleTap.numberOfTapsRequired = 1;
//    [self.tableView addGestureRecognizer:singleTap];
}
//- (void)handleSingleTapFrom:(UITapGestureRecognizer *)recognizer{
//    
//    [self.view endEditing:YES];
//}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    
    PywPayPwdSetProblemViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[PywPayPwdSetProblemViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.delegate = self;
    }
    
    if (indexPath.row == 0) {
        cell.answerIsDatePicker = YES;
        self.cell.placeholder = self.placeholderArray[0];
    } else {
        cell.answerIsDatePicker = NO;
        self.cell.placeholder = self.placeholderArray[1];
    }
    cell.problem = self.titleArray[indexPath.row];
    self.cell = cell;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
#pragma mark - PywPayPwdSetProblemViewCellDelegate
- (void)hiddenDatePicker{
    [self.cell removeDatePicker];
}
- (void)endEditing{
    [self.view endEditing:YES];
}
#pragma mark - PywPayPwdFooterViewDelegate
- (void)payPwdFooterView:(PywPayPwdFooterView *)footerView didSelectedButton:(UIButton *)setButton{
    
    PLog(@"立即设置====交易密码为：%@",self.payPwd);
}

#pragma mark - Lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = RGB(232, 232, 232, 1);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        
        PywPayPwdHeaderView *headerView = [[PywPayPwdHeaderView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
        headerView.userInteractionEnabled = YES;
        self.payPwd = headerView.payPwd;
        _tableView.tableHeaderView = headerView;
        
        PywPayPwdFooterView *footerView = [[PywPayPwdFooterView alloc] initWithFrame:CGRectMake(0, 0, 0, 100) Delegate:self ButtonTitle:@"立即设置"];
        footerView.userInteractionEnabled = YES;
        _tableView.tableFooterView = footerView;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
#pragma mark - Setter & Getter
- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"我的生日",@"我的家乡",@"小学校名",@"父亲名字"];
    }
    return _titleArray;
}
- (NSArray *)placeholderArray{
    if (!_placeholderArray) {
        _placeholderArray = @[@"请选择日期",@"请输入答案"];
    }
    return _placeholderArray;
}

@end
